#ifndef SPEECHSERVERNODE_H
#define SPEECHSERVERNODE_H
#include<string>
using namespace std;
//Ros tools
#include<actionlib/server/simple_action_server.h>
#include<sensor_msgs/JointState.h>
//Network Configurations
#define SPEECHSERVER_PORT 9876

//Actions
#include"speech_server/GetSpeechAction.h"
#include"speech_server/GetSpeech_JustAction.h"
#include"speech_server/GetSpeech_IgnoreAction.h"
#include"speech_server/GetSpeech_yesnoAction.h"
#include"speech_server/SetRecognitionConfidence.h"
#include"speech_server/Say.h"

//Services
#include"speech_server/LoadGrammar.h"
#include"speech_server/UnLoadGrammar.h"
#include"speech_server/UnLoadAllGrammar.h"
#include"speech_server/StartRecognition.h"
#include"speech_server/StopRecognition.h"
//My tools
#include"speech_server/Communication.h"
#include"XmlProccessor.h"

#include "workingthread.hpp"

//Type Defines
    typedef actionlib::SimpleActionServer<speech_server::GetSpeechAction> GetSpeechAction;
    typedef actionlib::SimpleActionServer<speech_server::GetSpeech_IgnoreAction> GetSpeechIgnoreAction;
    typedef actionlib::SimpleActionServer<speech_server::GetSpeech_JustAction> GetSpeechJustAction;
    typedef actionlib::SimpleActionServer<speech_server::GetSpeech_yesnoAction> GetSpeechYesNoAction;
//

class SpeechServerNode: public WorkingThread
{
Q_OBJECT
private:
    //ros Variables
    ros::NodeHandle *nh;
    ros::ServiceServer LoadGrammarService;
    ros::ServiceServer UnLoadGrammarService;
    ros::ServiceServer UnLoadAllGrammarService;
    ros::ServiceServer SetRecognitionConfidenceService;
    ros::ServiceServer SayService;

    //actions
    GetSpeechAction         *get_Speech_Action;
    GetSpeechIgnoreAction   *get_Speech_Ignore_Action;
    GetSpeechJustAction     *get_Speech_Just_Action;
    GetSpeechYesNoAction    *get_Speech_yesno_Action;
    ros::Publisher position_publisher;
    //tts Functions
    bool SayText(std::string);
    bool SayText(char*);

    //Verifying Functions (Asking the The Recognized Text is True or not and get Yes or No)
    bool VerifyText(std::string str);

    //Services Callback
    bool loadGrammarCB(speech_server::LoadGrammar::Request&,
                       speech_server::LoadGrammar::Response&);
    bool unLoadGrammarCB(speech_server::UnLoadGrammar::Request&,
                         speech_server::UnLoadGrammar::Response&);
    bool unLoadAllGrammarCB(speech_server::UnLoadAllGrammar::Request&,
                            speech_server::UnLoadAllGrammar::Response&);
    bool setRecognitionConfidenceCB(speech_server::SetRecognitionConfidence::Request&,
                                  speech_server::SetRecognitionConfidence::Response&);
    bool sayCB(speech_server::Say::Request&,speech_server::Say::Response&);



    //Actions Callback
    void GetSpeechActionExecuteCB(const speech_server::GetSpeechGoalConstPtr&);
    void GetSpeech_IgnoreActionExecuteCB(const speech_server::GetSpeech_IgnoreGoalConstPtr&);
    void GetSpeech_JustActionExecuteCB(const speech_server::GetSpeech_JustGoalConstPtr&);
    void GetSpeech_yesnoActionExecuteCB(const speech_server::GetSpeech_yesnoGoalConstPtr&);

    //Convertor Functions
    speech_server::SpeechRecognitionResult SpeechResultToRosSpeechResult(Result res);
    //info functions
    void ShowInfo(std::string info);
    void ShowInfoAndRosInfo(std::string info);

    void publishJointState(float pos);

    volatile bool running;
public:
    bool loadGrammar(std::string grammarname);
    bool unLoadGrammar(std::string);
    bool unLoadAllGrammar();
    bool setRecognitionConfidence(int confidence);
    bool say(std::string);
    void stop();

Q_SIGNALS:
    void Info(QString);
    void rosShutdown();

public:
    SpeechServerNode(ComminucationHandler *handler);
    ~SpeechServerNode();


    //Running Functions
    //Intializes Communication, Services and Actions
    bool init(int argc,char * argv[]);
    //Starts The Node to Spin
protected:
    void run();

};
#endif
